package com.nogroup.springbootproject.controller;

import com.nogroup.springbootproject.dto.ClassroomDto;
import com.nogroup.springbootproject.dto.CourseDto;
import com.nogroup.springbootproject.entity.Classroom;
import com.nogroup.springbootproject.entity.Course;
import com.nogroup.springbootproject.service.CourseService;
import com.nogroup.springbootproject.utils.PageUtils;
import com.nogroup.springbootproject.utils.result.DataResult;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Course)表控制层
 *
 * @author makejava
 * @since 2024-04-07 18:45:32
 */
@RestController
@RequestMapping("course")
public class CourseController {
    /**
     * 服务对象
     */
    @Resource
    private CourseService courseService;

    /**
     * 分页查询
     *
     * @param course 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @GetMapping
    public ResponseEntity<Page<Course>> queryByPage(Course course, PageRequest pageRequest) {
        return ResponseEntity.ok(this.courseService.queryByPage(course, pageRequest));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<Course> queryById(@PathVariable("id") String id) {
        return ResponseEntity.ok(this.courseService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param course 实体
     * @return 新增结果
     */
    @RequestMapping("courseInsert")
    @PostMapping
    public DataResult courseInsert(Course course) {
        System.out.println("course:" + course.toString());
        return DataResult.successByData(this.courseService.insert(course));
    }

    /**
     * 编辑数据
     *
     * @param course 实体
     * @return 编辑结果
     */
    @RequestMapping("edit")
    @PutMapping
    public DataResult edit(Course course) {
        return DataResult.successByData(this.courseService.update(course));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @RequestMapping("deleteById")
    @DeleteMapping
    public DataResult deleteById(Course course) {
        return DataResult.successByData(this.courseService.deleteById(course));
    }

    @RequestMapping("getAllCourses")
    public DataResult getAllCourses(){
        List<Course> courses = courseService.getAllCourses();
        return DataResult.successByDatas(courses);
    }

    @RequestMapping("getCoursesByFuzzySearch")
    public DataResult getCoursesByFuzzySearch(CourseDto courseDto){
        long count = courseService.countPage(courseDto);
        int index = PageUtils.getStartPage(courseDto.getPage(), courseDto.getLimit());
        courseDto.setPage(index < count ? index : 0);
        List<Course> courses = courseService.getCoursesByFuzzySearch(courseDto);
        return DataResult.successByTotalData(courses,count);
    }
}

